//
//  FMDetailViewController.m
//  LastTemp
//
//  Created by dllo on 15/4/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FMDetailViewController.h"
#import "FMDetailModel.h"
#import "NetWorkingTool.h"
#import "FMDetailCell.h"
#import "AVSingleton.h"
#import "bofangViewController.h"
#import "BassModel.h"
#import "AFSoundManager.h"
@interface FMDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *FMDetailArr;
@property(nonatomic, retain)UITableView *FMDetailTableView;
@property(nonatomic, retain)NSMutableArray *passPlayArr;
@property(nonatomic, retain)UIActivityIndicatorView *activity;
@end

@implementation FMDetailViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.FMDetailArr = [NSMutableArray array];
        self.title = @"歌曲列表";
    }
    return self;
}
- (void)dealloc
{
//    [_FMDetailArr release];
    [_FMDetailTableView release];
    [_activity release];
    [super dealloc];
}
// 解析数据
- (void)FMDetailData{
    if ([AVSingleton shareSingleton].internetNum == 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在没有网络,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    } else {
    NSString *str = [NSString stringWithFormat:@"http://fm.api.ttpod.com/vipradiosong?userid=0000001&num=150&tagid=%@&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.0.2&active=1&net=2&openudid=50a40f05044a0e2462e11cbf7d6612844b6c0008&idfa=058C286B-AB2C-41E2-A7FE-B67B3496FBBC&utdid=VQKI/UR603ADANyHaLs5uRrS&alf=201200", self.passTag_id];
    [NetWorkingTool netWorkingToolStr:str block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableArray *arr = [NSMutableArray array];
        arr = dic[@"data"];
        for (NSInteger i = 0; i < arr.count; i++) {
            FMDetailModel *fmDetailModel = [[FMDetailModel alloc] init];
            [fmDetailModel setValuesForKeysWithDictionary:arr[i]];
            fmDetailModel.url = [[arr[i] objectForKey:@"url_list"] firstObject][@"url"];
            [self.FMDetailArr addObject:fmDetailModel];
            [fmDetailModel release];
        }
        [self.FMDetailTableView reloadData];
        [self.activity stopAnimating];
    }];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"39"]];
    [self FMDetailData];
    
    self.FMDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.FMDetailTableView];
    [_FMDetailTableView release];
    
    self.FMDetailTableView.backgroundColor = [UIColor clearColor];
    self.FMDetailTableView.delegate = self;
    self.FMDetailTableView.dataSource = self;
    // 右button
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"player" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightButton:)];
//    UIButton *RightButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    RightButton.frame = CGRectMake(0, 0, 64, 64);
//    [RightButton setBackgroundImage:[UIImage imageNamed:@"bb.gif"] forState:UIControlStateNormal];
////    [RightButton setBackgroundImage:[UIImage imageNamed:@"dd"] forState:UIControlStateNormal];
//    [RightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:RightButton] autorelease];
    
    // 进度轮
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    [_activity release];

}
#pragma mark 右button点击方法
- (void)clickRightButton:(UIBarButtonItem *)button{
    bofangViewController *playVC = [[bofangViewController alloc] init];
    playVC.allArr = [AVSingleton shareSingleton].fromArr;
    [self presentViewController:playVC animated:YES completion:^{
    }];
    [playVC release];
}
//#pragma mark 右button点击方法
//- (void)clickRightButton:(UIBarButtonItem *)button{
//    bofangViewController *playVC = [[bofangViewController alloc] init];
//    playVC.allArr = [AVSingleton shareSingleton].fromArr;
//    [self presentViewController:playVC animated:YES completion:^{
//    }];
//    [playVC release];
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.FMDetailArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    FMDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[FMDetailCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.MyImageView.image = [UIImage imageNamed:@"m5"];
    cell.song_nameLabel.text = [self.FMDetailArr[indexPath.row] song_name];
    cell.singer_name.text = [self.FMDetailArr[indexPath.row] singer_name];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.passPlayArr = [NSMutableArray array];
    FMDetailModel *fm = self.FMDetailArr[indexPath.row];
    if ([AVSingleton shareSingleton].fromNum != indexPath.row || [AVSingleton shareSingleton].fromNum != 0) {
        [[AFSoundManager sharedManager] stop];
    }
    [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:fm.url andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
        [AVSingleton shareSingleton].percentage = percentage;
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"mm:ss"];
        NSDate *elapsedTimeDate = [NSDate dateWithTimeIntervalSince1970:elapsedTime];
        [AVSingleton shareSingleton].beginTime = [formatter stringFromDate:elapsedTimeDate];
        
        NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
        [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];
        
        
    }];
    for (NSInteger i = 0; i < self.FMDetailArr.count; i++) {
        BassModel *baseModel = [[BassModel alloc] init];
        FMDetailModel *fm1 = self.FMDetailArr[i];
        baseModel.name1 = fm1.song_name;
        baseModel.singerName1 = fm1.singer_name;
        baseModel.url1 = fm1.url;
        [self.passPlayArr addObject:baseModel];
        [baseModel release];
    }
    [AVSingleton shareSingleton].fromNum = indexPath.row;
    [AVSingleton shareSingleton].fromArr = self.passPlayArr;
    [AVSingleton shareSingleton].fromNum = indexPath.row;
    bofangViewController *play = [[bofangViewController alloc] init];
    [self presentViewController:play animated:YES completion:^{
        
    }];
 
    [AVSingleton shareSingleton].myBool = YES;

   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
